home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / DependentsCollection.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  3.6 KB  |  100 lines

  1. " --------------------------------------------------------------
  2.   A DependentsCollection is a collection of dependents for some 
  3.   object.  Instances forward update messages to the dependents, 
  4.   which are the elements of the collection.  Note that the same 
  5.   dependent may appear more than once in the collection.  Note 
  6.   also that the size of a DependentsCollection must always be 
  7.   2 or greater.  (If an object has only one dependent, that 
  8.   object by itself serves as the collection of dependents.)
  9.   --------------------------------------------------------------
  10. "
  11. Class DependentsCollection :Array
  12. [
  13.    asDependentsAsCollection
  14.      " Answer the receiver, considered as a collection of 
  15.      * dependents, as a real Collection.  Since the receiver 
  16.      * is a Collection already, answer the receiver. 
  17.      "
  18.      ^ self
  19. |
  20.    asDependentsWith: anObject
  21.      " Answer the receiver, considered as a collection of 
  22.      * dependents, with anObject added. 
  23.      "
  24.      ^ self grow: anObject
  25. |
  26.    asDependentsWithout: anObject ! newCollection item size indx !
  27.      " Answer the receiver, considered as a collection of 
  28.      * dependents, with the first occurrence of anObject 
  29.      * (if any) removed.  If anObject does not occur in 
  30.      * the receiver, answer the receiver. 
  31.      * If there is only one dependent left, just answer it, 
  32.      * rather than a new Collection. 
  33.      "
  34.      size <- self size.
  35.      
  36.      (size = 2)
  37.         ifTrue: [((self at: 1) == anObject)
  38.                    ifTrue: [ ^ self at: 2]
  39.  
  40.                   ifFalse: [(self at: 2) == anObject
  41.  
  42.                               ifTrue: [ ^ self at: 1]
  43.                              ifFalse: [ ^ self ] ] ].
  44.  
  45.      newCollection <- self class new: size - 1.
  46.      indx          <- 1.
  47.  
  48.      " Search until we find an occurrence of anObject, or 
  49.      * until we have copied all but the last item in the 
  50.      * collection. 
  51.      "
  52.      [indx = size or: [item <- self at: indx. item == anObject]]
  53.         whileFalse: [ newCollection at: indx put: item.
  54.                       indx <- indx + 1 ].
  55.      (indx = size)
  56.         ifTrue: [((self at: size) == anObject)
  57.                      ifTrue: [ ^ newCollection ]
  58.                     ifFalse: [ ^ self ] ]
  59.        
  60.        ifFalse: [newCollection replaceFrom: indx to: size - 1 
  61.                                       with: self startingAt: indx + 1.
  62.                  ^ newCollection ]
  63. |
  64.    performUpdate: aSymbol
  65.      " Send aSymbol to each member of the receiver. "
  66.  
  67.      (1 to: self size) do: [:i | (self at: i) perform: aSymbol ]
  68. |
  69.    performUpdate: aSymbol with: anObject
  70.      " Send aSymbol to each member of the receiver with 
  71.      * anObject as argument.
  72.      "
  73.      (1 to: self size) do: [:i | (self at: i) perform: aSymbol with: anObject]
  74. |
  75.    update: anAspect with: aParameter from: anObject
  76.      " Send the message update: anAspect with: aParameter
  77.      * from: anObject to each member of the receiver.
  78.      "
  79.      (1 to: self size) do: [:i | (self at: i) update: anAspect 
  80.                                                 with: aParameter from: anObject]
  81. |
  82.    updateRequest
  83.      " Send the message updateRequest to each member of the receiver.
  84.      * If any member answers false, answer false; otherwise, answer true.
  85.      "
  86.      (1 to: self size) do: [:i | (self at: i) updateRequest ifFalse: [ ^ false ] ].
  87.  
  88.      ^ true
  89. |
  90.    updateRequest: anAspectSymbol
  91.      " Send the message updateRequest: to each member of the 
  92.      * receiver with anAspectSymbol as argument.  If one 
  93.      * answers false, answer false, otherwise answer true.
  94.      "
  95.      (1 to: self size) do: [:i | ((self at: i) updateRequest: anAspectSymbol)
  96.                                    ifFalse: [ ^ false ] ].
  97.      ^ true
  98. ]
  99.  
  100.